package com.vidaappv1;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class AccountBalance extends Activity {

    // Progress Dialog
    private ProgressDialog pDialog;
    
    TextView txtBalance;
    int success;
    // url to create new product
    private static String url_get_balance = "http://192.168.43.191/vida/get_balance.php";
  
    JSONParser jsonParser = new JSONParser();
    // JSON Node names
    private static final String TAG_SUCCESS = "success";
        
    private static int balance;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_account_balance);
        		GetBalance newTask = new GetBalance();
        		newTask.execute();	        
   
        Button accountinfo = (Button)findViewById(R.id.button1);        
		Button placeanorder = (Button)findViewById(R.id.btnOrder_bal);
		Button topupcredits = (Button)findViewById(R.id.btnTopup);
		
		accountinfo.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent accountinfointent = new Intent(getApplicationContext(),AccountInfo.class);
				startActivity(accountinfointent);
			}
		});
		
		placeanorder.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent placeorderintent = new Intent(getApplicationContext(),BigMenu.class);
				startActivity(placeorderintent);
				
			}
		});
		
		topupcredits.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent topupintent = new Intent(getApplicationContext(),LoadCredits.class);
				startActivity(topupintent);
				
			}
		});
	
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.account_balance, menu);
		return true;
	}

    class GetBalance extends AsyncTask<String,String,String>
    {


            @Override
            protected void onPreExecute() {
                    // TODO Auto-generated method stub
                    super.onPreExecute();
                    setContentView(R.layout.activity_account_balance);
                    txtBalance= (TextView)findViewById(R.id.txtBalance);
            pDialog = new ProgressDialog(AccountBalance.this);
            pDialog.setMessage("Fetching,Balance...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
            }
            
            @Override
            protected String doInBackground(String... arg0) {
                    // TODO Auto-generated method stub
                     List<NameValuePair> params = new ArrayList<NameValuePair>();
             params.add(new BasicNameValuePair("pid", "5"));
                    
                // getting JSON Object
                // Note that create product url accepts POST method
             JSONObject json = jsonParser.makeHttpRequest(url_get_balance,
                        "POST", params);
                // check log cat fro response
                Log.d("Create Response", json.toString());
     
                // check for success tag
                try {
                    success = json.getInt(TAG_SUCCESS);
                    balance = json.getInt("balance");
                   
                } catch (JSONException e) {
                    e.printStackTrace();
                }
     
             return null;
            }
    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
    	if(success==1){
    		txtBalance.setText("John,your current account Balance is:R"+balance);
    	}
    		// dismiss the dialog once done
        pDialog.dismiss();
    }
    
    }

}
